<?php
namespace App\Models\V5;
use App\Models\V5\CompanyModel;
use DB;

class CompanyQuestionModel extends CompanyModel{


    /**
     * 获得公司众智共享的类型
     * @param integer $companyId 公司的ID
     * */
    public function get_question_types($companyId)
    {
        $questionTypes = DB::table('company_question_type')
            ->select('id','type_name','descript','img')
            ->where('company_id',$companyId)
            ->where('deleted_at')
            ->get();
        return $questionTypes;
    }


    /**
     * 添加问题
     * @param integer $question 问题文字内容
     * @param array   $imgs     问题图片数组
     * @param integer $type     问题类型
     * @param integer $userId   用户ID
     * @param integer $companyId   公司ID
     * @param integer $readPartyId 读书会ID
     * @param integer $isAnonymous 是否匿名
     * */
    public function add_question($question,array $imgs,$type,$userId,$companyId,$readPartyId,$isAnonymous)
    {
        $date   = date_time();
        $data   = [
            'company_id'    => $companyId,
            'read_party_id' => $readPartyId,
            'user_id'       => $userId,
            'type'          => $type,
            'question'      => $question,
            'is_anonymous'  => $isAnonymous,
            'imgs'          => implode(',',$imgs),
            'created_at'    => $date,
            'updated_at'    => $date
        ];
        $id = DB::table('company_question')->insertGetId($data);
        return $id;
    }

    /**
     * 获得公司所有问题
     * @param integer $companyId 公司ID
     * @param integer $userId 用户ID
     * @param integer $isAnswed 是否选择已有答案的
     * @param integer $questionType 问题类型
     * */
    public function get_questions($companyId,$userId = 0,$isAnswed = -1,$questionType = 0)
    {
        $db = DB::table('company_question as a')
            ->leftJoin('company_question_type as b','b.id','=','a.type')
            ->leftJoin('memberinfo as d','d.UserID','=','a.user_id')
            ->select(
                "a.id",
                "a.user_id",
                "a.type",
                "b.type_name",
                "a.question",
                "a.is_anonymous",
                "a.answer_id",
                "a.created_at",
                "a.imgs",
                "d.FilePath1 as head_img",
                "d.YourName")
            ->where('a.company_id',$companyId)
            ->where('a.deleted_at')
            ->where('b.deleted_at');

        $userId    > 0    ? $db->where('a.user_id',$userId)     : '';

        if($isAnswed == 0)
        {
            $db->where('a.answer_id',$isAnswed);
        }
        elseif($isAnswed == 1)
        {
            $db->where('a.answer_id','>',0);
        }

        $questionType > 0 ? $db->where('a.type',$questionType)  : '';
        $questions  = $db->orderBy('a.id','desc')->paginate(10);

        foreach($questions as $quest)
        {
            $quest->question    = emoji_text_decode($quest->question);
            if($quest->imgs)
            {
                $quest->imgs =explode(',',$quest->imgs);
            }
            else
            {
                $quest->imgs = [];
            }
        }
        return $questions;
    }


    /**
     * 获得问题详解
     * */
    public function get_question_detail($questionId)
    {
        $questionInfo = DB::table('company_question as a')
            ->leftJoin('memberinfo as b','b.UserID','=','a.user_id')
            ->leftJoin('company_question_type as d','d.id','=','a.type')
            ->select('a.id','a.user_id','a.question','a.imgs','a.created_at','a.answer_id','a.is_anonymous','b.YourName','b.FilePath1 as head_img','d.type_name')
            ->where('a.id',$questionId)
            ->where('a.deleted_at')
            ->first();
        $questionInfo->head_img = change_member_head_img($questionInfo->head_img);
        $questionInfo->imgs     = $questionInfo->imgs ? explode(',',$questionInfo->imgs) : [];
        return $questionInfo;
    }

    /**
     * 获得提问得到的答案
     * @param integer $questionId 问题ID
     * @param integer $userId   用户ID
     * */
    public function get_question_answer($questionId,$userId = 0)
    {

        $answers = $db = DB::table('company_question_answer as a')
            ->leftJoin('memberinfo as b','b.UserID','=','a.user_id')
            ->leftJoin('memberinfo as d','d.UserID','=','a.replyed_user_id')
            ->select(
                'a.id',
                'a.parent_id',
                'a.answer',
                'a.imgs',
                'a.is_accept',
                'a.created_at',
                'a.user_id',
                'a.is_anonymous',
                'a.like_users',
                DB::raw("FIND_IN_SET($userId,a.like_users) is_like"),
                'b.YourName',
                'b.FilePath1 as head_img',
                DB::raw("IFNULL(d.YourName,'') as replyed_name"),
                'd.FilePath1 as replyed_head_img'
                )
            ->where('question_id',$questionId)
            ->where('deleted_at')
            ->orderBy('a.id','asc')
            ->get();
        foreach($answers as $ans)
        {
            $ans->likeNum           = count( $ans->like_users ? explode(',',$ans->like_users) : []);
            $ans->head_img          = change_member_head_img($ans->head_img);
            $ans->replyed_head_img  = change_member_head_img($ans->replyed_head_img);
            $ans->imgs              = $ans->imgs ? explode(',',$ans->imgs) : [];
            $ans->answer            = emoji_text_decode($ans->answer);
        }
        return $answers;
    }


    /**
     * 统计问题被回答的数量
     * @param
     * */
    public function count_question_answer($questionId)
    {
        $num = DB::table('company_question_answer')
            ->where('question_id',$questionId)
            ->where('parent_id',0)
            ->where('deleted_at')
            ->count();
        return $num;
    }

    /**
     * 获得顶级回答的用户
     * @param integer $questionId 问题ID
     * */
    public function get_top_answer_user($questionId)
    {
        $users = DB::table('company_question_answer')
            ->where('question_id',$questionId)
            ->where('parent_id',0)
            ->pluck('user_id')
            ->toArray();

        return $users;
    }


    /**
     * 添加评论
     * @param   integer $questionId 读书会ID
     * @param   string  $answer     回复的内容
     * @param   integer $answerId   被评论的回答ID
     * @param   integer $userId     用户ID
     * @param   integer $replyUserId 被回复的用户ID
     * @param   array   $imgs       回复的图片
     * @param   integer $isAnonymous 是否匿名
     * */
    public function add_answer($questionId,$answer,$answerId,$userId,$replyUserId,$imgs,$isAnonymous)
    {
        $imgs   = implode(',',$imgs);
        $time   = date_time();

        $data   = [
            'question_id'   => $questionId,
            'parent_id'     => $answerId,
            'user_id'       => $userId,
            'replyed_user_id'=>$replyUserId,
            'answer'        => $answer,
            'imgs'          => $imgs,
            'created_at'    => $time,
            'updated_at'    => $time,
            'is_anonymous'  => $isAnonymous
        ];
        $id = DB::table('company_question_answer')
            ->insertGetId($data);
        return $id;
    }


    /**
     * 采纳回复
     * @param integer $questionId   问题ID
     * @param integer $answerId     答案ID
     * */
    public function accept_answer($questionId,$answerId)
    {
        $res = DB::table('company_question_answer')
            ->where('id',$answerId)
            ->where('question_id',$questionId)
            ->update(['is_accept'=>1]);
        if($res)
        {
            DB::table('company_question')
                ->where('id',$questionId)
                ->update(['answer_id'=>$answerId]);
        }
    }

    /**
     * 获取单条回复信息
     * @param
     * */
    public function get_single_answer($answerId)
    {
        return DB::table('company_question_answer')->where('id',$answerId)->first();
    }


    /**
     * 添加或取消点赞
     * @param integer $userId 用户ID
     * @param integer $answerId 答案ID
     * */
    public function add_or_cancel_like($userId,$answerId)
    {
        $answerInfo = $this->get_single_answer($answerId);
        $users      = $answerInfo->like_users;
        $users      = explode(',',$users);
        if(in_array($userId,$users))
        {
            array_unset_value($users,$userId);
        }
        else{
            array_push($users,$userId);
        }
        $users      = implode(',',$users);
        $users      = trim($users,',');
        DB::table('company_question_answer')->where('id',$answerId)->update(['like_users'=>$users]);
    }
}